package com.nike.amoeba.bean.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.nike.amoeba.bean.db.MainDrawerBean
import com.nike.amoeba.bean.db.MainDrawerParentBean
import com.nike.amoeba.bean.db.UploadOrderBean
import kotlinx.coroutines.flow.Flow

/**
 * @Author nike
 * @Date 2023/7/28 10:34
 * @Description
 */
@Dao
interface MainDrawerParentDao {

    /**
     * 增加
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun add(vararg bean: MainDrawerParentBean)

    /**
     * 删除
     */
    @Delete
    suspend fun delete(bean: MainDrawerParentBean)

    //删除数据库表全部数据
    @Query("delete from MainDrawerParentBean")
    suspend fun deleteAll()

     /**
     * 查询所有
     */
    @Query("SELECT * FROM MainDrawerParentBean")
    fun findAll(): List<MainDrawerParentBean>

    @Query("SELECT * FROM MainDrawerParentBean WHERE type =:type")
    fun findId(type: Int): MainDrawerParentBean

//    @Query("SELECT * FROM MainDrawerParentBean INNER JOIN MainDrawerBean ON MainDrawerParentBean.type = MainDrawerBean.parentType WHERE MainDrawerBean.parentType = :parentType")
//    fun findChildAll(parentType: Int): List<MainDrawerBean>

    @Query("SELECT * FROM MainDrawerParentBean INNER JOIN MainDrawerBean ON MainDrawerParentBean.type = MainDrawerBean.parentType WHERE MainDrawerBean.parentType = :parentType AND MainDrawerBean.userId = :userId AND MainDrawerBean.companyId = :companyId")
    fun findChildAll(parentType: Int, userId: String?, companyId: String?): List<MainDrawerBean>
    /**
     * 根据用户查询所有
     */
    @Query("SELECT * FROM MainDrawerParentBean WHERE userId =:roNo AND companyId =:companyId")
    fun findAll(roNo: String, companyId: String): Flow<List<MainDrawerParentBean>>

    /**
     * 更新
     */
    @Update
    fun update(bean: MainDrawerParentBean)


    suspend fun insertOrUpdate(bean: MainDrawerParentBean) {
        if (bean == null){
            add(bean)
        }else{
            update(bean)
        }
    }

}